package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    CheckGroupDao checkGroupDao;



    @Transactional(readOnly = false)
    @Override
    public Result add(CheckGroup checkGroup,Integer[] checkItemIds) {
        try{
             checkGroupDao.add(checkGroup);
            for (Integer checkItemId : checkItemIds) {
                checkGroupDao.addCheckItemId(checkGroup.getId(),checkItemId);
            }
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    @Transactional(readOnly = false)
    @Override
    public Result update(CheckGroup checkGroup,Integer[] checkItemIds) {
        try{
            checkGroupDao.update(checkGroup);

            //删除与该检查组原关联的检查项
            checkGroupDao.deleteCheckItemId(checkGroup.getId());

            //添加新的关联检查项
            for (Integer checkItemId : checkItemIds){
                checkGroupDao.addCheckItemId(checkGroup.getId(),checkItemId);
            }
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }


    @Transactional(readOnly = false)
    @Override
    public Result delete(Integer id) {
        try{

            //删除与该检查组原关联的检查项
            checkGroupDao.deleteCheckItemId(id);

            checkGroupDao.delete(id);

            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }


    @Override
    public Result queryById(Integer id) {

        try{
            CheckGroup checkGroup = checkGroupDao.queryById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    /**
     * 分页或者查询功能
     * @param queryPageBean
     * @return
     */
    @Override
    public Result queryPage(QueryPageBean queryPageBean) {
        try{

            Page<CheckGroup> page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            checkGroupDao.queryAllByQueryString(queryPageBean.getQueryString());
            PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    @Override
    public Result queryAll() {
        try{

            List<CheckGroup> checkGroups = checkGroupDao.queryAll();

            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

}
